package com.migrator.db;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Properties;

import com.migrator.ctes.Driver;
import com.migrator.utils.Database;

public class PostgreSQL extends Database {
	

        
	public String processType(String type,int size, int scale)
	  {
	    String usigned = "UNSIGNED";
	    int i = type.indexOf(usigned);
	    if( i!=-1 )
	      type = type.substring(0,i)+type.substring(i+usigned.length());
	    
	    if( type.equalsIgnoreCase("varchar") && (size==65535) )
	      type = "TEXT";
	    
	    return type.trim();
	  }
	
	@Override
	public Collection<String> listCatalogs() {
		// TODO Auto-generated method stub
		Collection<String> result = new ArrayList<String>() ;
		
		try {
			ResultSet rs = this.executeQuery("SELECT * FROM pg_database") ;
		
			
			while(rs.next()){
				result.add(rs.getString("datname")) ;
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return result ;
	}


	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		

		PostgreSQL  ps = new PostgreSQL();
		Properties prop = new Properties() ;
		prop.setProperty("user", "root") ;
		prop.setProperty("password", "passer") ;
		try {
			ps.connect(Driver.POSTGRESQL_DRIVER, "jdbc:postgresql://localhost:5432/postgres" , prop) ;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		for(String table : ps.listCatalogs()){
			System.out.println(table);	
		}
		
		ps.close();

	}

}
